<template>
  <dialog-editor ref="dialog" :width="50"></dialog-editor>
</template>

<script>
import DialogEditor from '@/components/DialogEditor'
import dialogMixins from '@/utils/commonDialogMixins'
import {mRequest} from '@/utils/request';
import {btnBlur} from '@/utils'

export default {
  mixins: [dialogMixins],
  components: {DialogEditor},

  data() {
    return {
      bottleAndCapMap: {},
      deepCategoryMap: {},
      dataFormConfig: {
        id: {
          hidden: true
        },
        contractTime: {
          label: '采购合同日期',
          type: 'date'
        },
        taxInclusive: {
          label: '含税价',
          type: 'radio',
          radioBorder: false,
          radioList: [
            {label: 0, text: '不含税'},
            {label: 1, text: '含税'}
          ],
          default: 0
        },
        purchaseType: {
          label: '采购类型',
          type: 'radio',
          radioBorder: true,
          radioList: [
            {text: '模具', label: 6},
            {text: '商品', label: 14},
          ],
          changedCallback: this.purchaseTypeChangedHandler,
          default: 14
        },
        supplierId: {
          label: '供应商',
          list: [],
          type: 'list',
          placeholder: '请选择客户'
        },
        contractNo: {
          label: '合同编号',
          disabled: true
        },
        deliveryTime: {
          label: '交付日期',
          type: 'date'
        },
        totalAmount: {
          label: '总金额',
          disabled: true
        },
        remark: {
          label: '备注',
          inputType: 'textarea',
          placeholder: '请输入备注'
        },
        status: {
          label: '合同状态',
          type: 'radio',
          radioBorder: false,
          radioList: [
            {label: 0, text: '草稿'},
            {label: 3, text: '已下单'}
          ],
          default: 0
        },
        tableListDataModel: {
          type: 'table',
          reply: () => this.R.equals(6, this.dataForm['purchaseType']),
          column: {
            modelId: {
              label: '模具编号',
              width: 120
            },
            price: {
              label: '单价'
            },
            successMo: {
              label: '成模数量'
            },
            primaryMo: {
              label: '初模数量'
            },
            menTou: {
              label: '闷头数量'
            },
            louDou: {
              label: '漏斗数量'
            },
            kouZi: {
              label: '口子数量'
            },
            xinZi: {
              label: '芯子数量'
            },
            qiTou: {
              label: '气头数量'
            },
            jiaZi: {
              label: '夹子数量'
            },
            lengQue: {
              label: '冷却数量'
            },
            remark: {
              label: '备注'
            }
          },
          operate: {
            addOneLine: {
              label: '新增采购信息',
              icon: 'el-icon-edit',
              type: 'primary',
              clickHandler: this.addNewPurchaseModel
            },
            deleteOneLine: {
              label: '删除选中采购信息',
              icon: 'el-icon-delete',
              type: 'danger',
              clickHandler: this.deletePurchaseModel
            }
          }
        },
        tableListDataBottleCap: {
          type: 'table',
          reply: () => this.R.equals(14, this.dataForm['purchaseType']),
          column: {
            productNo: {
              label: '商品信息',
              width: 120
            },
            unitPrice: {
              label: '单价'
            },
            purchaseNumber: {
              label: '数量'
            },
            totalPrice: {
              label: '总价'
            },
            remark: {
              label: '备注'
            }
          },
          operate: {
            addOneLine: {
              label: '新增采购信息',
              icon: 'el-icon-edit',
              type: 'primary',
              clickHandler: this.addNewPurchaseBottleCap
            },
            deleteOneLine: {
              label: '删除选中采购信息',
              icon: 'el-icon-delete',
              type: 'danger',
              clickHandler: this.deletePurchaseBottleCap
            }
          }
        }
      }
    }
  },
  methods: {
    initBottleAndCapMap() {
      mRequest('productApi/getProductInfoList', {}, (res) => {
        this.R.pathOr([], ['data', 'list'], res).forEach(item => {
          this.bottleAndCapMap[item.id] = item.name
        })
      })
    },
    contractTotalAmountSet(type) {
      if (this.R.isNil(type)) {
        return
      }
      let orderPriceList = []
      if (type === 6) {
        orderPriceList = this.dataForm.tableListDataModel.map(window.R.propOr(0, 'price'))
      }
      if (type === 14) {
        orderPriceList = this.dataForm.tableListDataBottleCap.map(window.R.propOr(0, 'totalPrice'))
      }
      this.dataForm.totalAmount = window.R.reduce(window.R.add, 0, orderPriceList)
    },
    addNewPurchaseModel(_, evt) {
      const cb = (newValue) => {
        this.dataForm.tableListDataModel.push(newValue)
        this.contractTotalAmountSet(6)
      }
      this.$parent.openPurchaseModelDialog(cb)
      btnBlur(evt)
    },
    addNewPurchaseBottleCap(_, evt) {
      const cb = (newValue) => {
        const {productId, unitPrice, purchaseNumber} = newValue
        newValue.productNo = this.bottleAndCapMap[productId]
        newValue.totalPrice = unitPrice * purchaseNumber
        this.dataForm.tableListDataBottleCap.push(newValue)
        this.contractTotalAmountSet(14)
      }
      this.$parent.openPurchasePartDialog(cb)
      btnBlur(evt)
    },
    deletePurchaseModel(selection, evt) {
      const selectionIndex = selection.map(item => this.dataForm.tableListData.indexOf(item)).sort((a, b) => {
        return a - b;
      })

      for (let i = selectionIndex.length - 1; i >= 0; i--) {
        this.dataForm.tableListDataModel.splice(selectionIndex[i], 1)
      }
      btnBlur(evt)
    },
    deletePurchaseBottleCap(selection, evt) {
      const selectionIndex = selection.map(item => this.dataForm.tableListData.indexOf(item)).sort((a, b) => {
        return a - b;
      })

      for (let i = selectionIndex.length - 1; i >= 0; i--) {
        this.dataForm.tableListDataBottleCap.splice(selectionIndex[i], 1)
      }
      btnBlur(evt)
    },
    purchaseTypeChangedHandler() {
      this.initSupplierId()
      this.contractTotalAmountSet(this.dataForm.purchaseType)
    },
    initSupplierId() {
      mRequest('supplierApi/getSupplierList', {}, (res) => {
        this.dataFormConfig['supplierId'].list = this.R.pathOr([], ['data', 'list'], res)
      })
    },
    init(id) {
      this.dataForm.id = id || 0
      this.initSupplierId()
      this.initBottleAndCapMap()

      this.$refs.dialog.initDialog({
        dataForm: this.dataForm,
        config: this.dataFormConfig,
        dataRule: this.getNeedDataRule('purchaseType', 'contractTime', 'supplierId', 'deliveryTime', 'status'),
        initApi: 'contractApi/contractPurchaseInfoInfo',
        saveApi: () => {
          if (this.R.equals(14, this.dataForm['purchaseType'])) {
            return 'contractApi/contractPurchasePartDetailSave'
          }
          if (this.R.equals(6, this.dataForm['purchaseType'])) {
            return 'contractApi/contractPurchaseModelDetailSave'
          }
          return 'contractApi/contractPurchaseAndDetailSave'
        },
        updateApi: 'contractApi/contractPurchaseAndDetailUpdate',
        objectName: 'contractPurchaseInfo'
      })
    }
  }
}
</script>


